<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

namespace app\shortvideo\model;

use app\BaseModel;
use think\facade\Db;

class ShortvideoVisitors extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shortvideo_visitors';


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 09:50
     * @功能说明:获取统计数据
     */
    public function getCount($dis,$group='user_id'){

        $count = $this->where($dis)->group($group)->count();

        return $count;
    }

    /**
     **@param $where
     * @功能说明: 统计人数
     * @author lichuanming
     * @DataTime: 2020/5/19 15:29
     */
    public function getGroupCount($where){
        $count = $this->where($where)->group('user_id')->count();
        return $count;
    }

    /**
     * @param $dis
     * @功能说明:获取当天条数
     * @author chenniang
     * @DataTime: 2020-05-19 11:12
     */
    public function getDayCount($dis,$group='user_id'){

        $count = $this->where($dis)->whereDay('create_time')->group($group)->count();
        return $count;

    }

    /**
     * @param $dis
     * @功能说明:获取当月条数
     * @author chenniang
     * @DataTime: 2020-05-19 11:12
     */
    public function getMonthCount($dis,$group='user_id'){

        $count = $this->where($dis)->whereMonth('create_time')->group($group)->count();

        return $count;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 11:34
     * @功能说明:获取视频列表
     */
    public function getVideoList($dis,$staff_id){

        $data = $this->alias('a')
                ->join('longbing_card_shortvideo b','a.video_id = b.id')
                ->where($dis)
                ->group('b.id')
                ->order('b.top desc,a.id desc')
                ->field(['b.cover','b.title','b.describe','b.id as video_id','b.top'])
                ->paginate(10)
                ->toArray();

        if(!empty($data['data'])){
            //模型
            $browse_model = new ShortvideoBrowse();

            foreach ($data['data'] as &$v){

                $count_dis = [
                    //视频
                    'video_id' => $v['video_id'],
                    //员工
                    'staff_id' => $staff_id,
                    //用户
//                    'type'     => 1
                ];
                //浏览数
                $v['browse_count'] = $browse_model->getCount($count_dis,'id');
                //获客人数
                $v['visitors_people'] = $this->getCount($count_dis);
                //转发数
                $dis['mode'] = 4;

                $v['send_count'] = $this->getCount($count_dis,'id');

            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 13:19
     * @功能说明:获客人数排行
     */
    public function getTopUserList($dis,$page = 10,$mapor=[]){

        $data = Db::name('longbing_card_user_info')->alias('a')
                ->join('longbing_card_shortvideo_visitors b','a.fans_id = b.staff_id AND type = 1')
                ->join( 'longbing_card_company c', 'a.company_id = c.id', 'LEFT' )
                ->join( 'longbing_card_company d', 'c.top_id = d.id', 'LEFT' )
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field(['a.fans_id','a.company_id','COUNT(distinct(b.user_id)) as get_count','c.name as company_name','d.name as top_company_name'])
                ->group('a.fans_id')
                ->order('get_count desc,fans_id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as $k=>&$v){

                if(empty($v['top_company_name'])){

                    $v['top_company_name'] = $v['company_name'];

                    $v['company_name']     = '未设置部门';
                }
                //员工头像
                $v['staff_avatar'] = lbUserAvatar($v['fans_id']);
                //员工昵称
                $v['staff_name']   = lbUserName($v['fans_id']);
                //排名
                $v['top']          = $k+1;
            }

        }

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-05-19 13:19
     * @功能说明:获客人数排行
     */
    public function getTopUserLimit($dis,$page = 10,$mapor=[]){

        $data = Db::name('longbing_card_user_info')->alias('a')
            ->join('longbing_card_shortvideo_visitors b','a.fans_id = b.staff_id AND type = 1')
            ->join( 'longbing_card_company c', 'a.company_id = c.id', 'LEFT' )
            ->join( 'longbing_card_company d', 'c.top_id = d.id', 'LEFT' )
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field(['a.fans_id','a.company_id','COUNT(distinct(b.user_id)) as get_count','c.name as company_name','d.name as top_company_name'])
            ->group('a.fans_id')
            ->order('get_count desc,fans_id desc')
            ->limit($page)
            ->select()
            ->toArray();

        if(!empty($data)){

            foreach ($data as $k=>&$v){

                if(empty($v['top_company_name'])){

                    $v['top_company_name'] = $v['company_name'];

                    $v['company_name']     = '未设置部门';
                }
                //员工头像
                $v['staff_avatar'] = lbUserAvatar($v['fans_id']);
                //员工昵称
                $v['staff_name']   = lbUserName($v['fans_id']);
                //排名
                $v['top']          = $k+1;
            }

        }

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-05-20 11:10
     * @功能说明:获客列表
     */

    public function peopleList($dis){

        $data = $this->alias('a')
            ->join('longbing_card_user b','a.user_id = b.id')
            ->where($dis)
            ->field('b.nickName,b.avatarUrl,a.create_time,a.id,a.user_id')
            ->group('a.user_id')
            ->order('create_time desc,a.id desc')
            ->paginate(10)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $dis[] = ['a.user_id','=', $v['user_id']];
                //次数
                $v['time'] = $this->alias('a')->where($dis)->count();

                array_pop($dis);
//                dump($dis);

                //浏览时间
                $f_time = datachange($v,'create_time');

                $v['create_time'] = $f_time['radar_group'].$f_time['radar_time'];

                $v['radar_group'] = $f_time['radar_group'];

                $v['radar_time']  = $f_time['radar_time'];

                $v['avatarUrl']   = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';

                $v['phone']       = Db::name('longbing_card_user_phone')->where(['user_id'=>$v['user_id']])->value('phone');

                $v['phone']       = !empty($v['phone'])&&$v['phone']!='null'?$v['phone']:'';

            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 14:04
     * @功能说明:添加
     */
    public function visitorsAdd($data){

        $data['create_time'] = time();

        $dis['user_id']  = $data['user_id'];

        $dis['staff_id'] = $data['staff_id'];

        $dis['first']    = 1;

        $first = $this->where($dis)->find();

        if(empty($first)&&!empty($data['type'])&&$data['type']==1){

            $data['first'] = 1;

        }else{

            $data['first'] = 0;
        }
        $res = $this->insert($data);

        return $res;
    }


}